import 'dart:ui';
import 'package:flutter/material.dart';
import 'package:flutter_jd/pages/tabbar/my/controller.dart';
import 'package:flutter_jd/routes/routing_constants.dart';
import 'package:flutter_jd/service/global_service.dart';
import 'package:flutter_jd/ui/widgets/kit.dart';
import 'package:get/get.dart';
import 'package:get/get_state_manager/get_state_manager.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/route_manager.dart';

// 用户区域
class UserWrap extends GetWidget<MyController> {
  @override
  Widget build(BuildContext context) {
    return Obx(() => _buildTopWrap());
  }

  Widget _buildTopWrap() {
    Widget avatarWidget = Container();
    GlobalConfigService globalService = Get.find();

    if (globalService.isLogin.value == true) {
      avatarWidget = GestureDetector(
        onTap: () {
          Get.toNamed(UserInfoRoute);
        },
        child: Container(
          child: Row(
            children: [
              Container(
                width: 50.r,
                height: 50.r,
                decoration: BoxDecoration(
                  color: Colors.white,
                  border: Border.all(
                    color: Colors.white,
                    width: 1.5.w,
                  ),
                  borderRadius: BorderRadius.circular(50.r),
                ),
                child: ClipRRect(
                  borderRadius: BorderRadius.circular(50.r),
                  child: Image.asset(
                    'assets/images/common/avatar.png',
                    fit: BoxFit.cover,
                  ),
                ),
              ),
              Container(
                margin: EdgeInsets.only(left: 15.w),
                child: Text(
                  '一颗稀仔',
                  style: TextStyle(
                    color: Colors.white,
                    fontSize: 14.sp,
                    fontWeight: FontWeight.w500,
                  ),
                ),
              ),
            ],
          ),
        ),
      );
    } else {
      avatarWidget = GestureDetector(
        onTap: () {
          Get.toNamed(LoginRoute);
        },
        child: Container(
          color: Colors.transparent,
          child: Row(
            children: [
              Container(
                width: 50.r,
                height: 50.r,
                decoration: BoxDecoration(
                  color: Color(0xFFF2F2F2),
                  border: Border.all(
                    color: Colors.white,
                    width: 1.5.w,
                  ),
                  borderRadius: BorderRadius.circular(50.r),
                ),
              ),
              Container(
                margin: EdgeInsets.only(left: 15.w),
                child: Text(
                  '登录/注册',
                  style: TextStyle(
                    color: Colors.white,
                    fontSize: 14.sp,
                    fontWeight: FontWeight.w500,
                  ),
                ),
              ),
            ],
          ),
        ),
      );
    }

    return Stack(children: [
      Container(
        width: double.infinity,
        height: 230.h,
        child: Image.asset(
          'assets/images/my/my_bg.png',
          fit: BoxFit.cover,
        ),
      ),
      Container(
        margin: EdgeInsets.only(
          top: (MediaQueryData.fromWindow(window).padding.top + 15).h,
          left: 15.w,
          right: 12.w,
        ),
        child: Column(
          children: [
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                avatarWidget,
                Container(
                  width: 90.w,
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.end,
                    children: [
                      GestureDetector(
                        onTap: () {
                          Get.toNamed(SettingRoute);
                        },
                        child: Container(
                          padding: EdgeInsets.only(right: 8.w, left: 8.w),
                          child: Kit.iconFont(0xe612, Colors.white, 20.sp),
                        ),
                      ),
                      GestureDetector(
                        onTap: () {
                          Get.toNamed(MessageRoute);
                        },
                        child: Container(
                          color: Colors.transparent,
                          padding: EdgeInsets.only(left: 8.w),
                          child: Kit.iconFont(
                            0xe8b8,
                            Colors.white,
                            20.sp,
                          ),
                        ),
                      )
                    ],
                  ),
                )
              ],
            ),
            Container(
              height: 60.h,
              margin: EdgeInsets.only(top: 45.h),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: List.generate(
                  controller.topList.length,
                  (index) => Container(
                    padding: EdgeInsets.only(
                      left: 10.w,
                      right: 10.w,
                    ),
                    child: Column(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        Text(
                          controller.topList[index]['num'].toString(),
                          style: TextStyle(
                            color: Colors.white,
                            fontSize: 18.sp,
                            fontWeight: FontWeight.w500,
                          ),
                        ),
                        Container(
                          margin: EdgeInsets.only(top: 5.h),
                          child: Text(
                            controller.topList[index]['title'],
                            style: TextStyle(
                              color: Colors.white,
                              fontSize: 12.sp,
                            ),
                          ),
                        )
                      ],
                    ),
                  ),
                ),
              ),
            ),
          ],
        ),
      ),
    ]);
  }
}
